#include "Net.hpp"
#include "Server/Rpc_router.hpp"
#include "Unit.hpp"
#include "Dispatch.hpp"
#include <memory>

// void Rpc_ReqMessageCBack(const Abstract::BaseConnection::ConPtr &Con, const Abstract::BaseMessage::MesaPtr &Mptr)
// {
//     // 把基类指针转成派生类指针方便调用派生类的方法
//     std::shared_ptr<Message::RpcRequest> RpcPtr = std::dynamic_pointer_cast<Message::RpcRequest>(Mptr);
//     DLOG("进入Rpc请求消息回调模块");
//     std::string body = Mptr->Serialize();
//     std::string uuid = Mptr->GetUuid();

//     std::cout << "接收的消息:" << body << std::endl;
//     auto Respon = Message::BuildMessage::build<Message::RpcReponceJson>();
//     Respon->Setcode(ResCode::ResCode_Ok);
//     Respon->SetMtype(Rs_RqType::RPC_Res);
//     Respon->SetUuid(uuid);
//     Respon->SetResult(33);
//     Con->Send(Respon);
// }
// void Topic_ReqMessageCBack(const Abstract::BaseConnection::ConPtr &Con, const Abstract::BaseMessage::MesaPtr &Mptr)
// {
//     DLOG("进入Topic请求消息回调模块");
//     std::string body = Mptr->Serialize();
//     std::string uuid = Mptr->GetUuid();
//     std::cout << "接收的消息:" << body << std::endl;
//     auto TopRespon = Message::BuildMessage::build<Message::TopicResponceJson>();
//     TopRespon->Setcode(ResCode::ResCode_Ok);
//     TopRespon->SetMtype(Rs_RqType::TOPIC_Res);
//     TopRespon->SetUuid(uuid);
//     Con->Send(TopRespon);
// }
// void Ser_ResMessageCBack(const Abstract::BaseConnection::ConPtr &Con, const Abstract::BaseMessage::MesaPtr &Mptr)
// {
//     DLOG("进入Server请求消息回调模块");
//     std::string body = Mptr->Serialize();
//     std::string uuid = Mptr->GetUuid();
//     std::cout << "接收的消息:" << body << std::endl;
//     auto Respon = Message::BuildMessage::build<Message::ServerReponceJson>();
//     Respon->Setcode(ResCode::ResCode_Ok);
//     Respon->SetMtype(Rs_RqType::Service_Res);
//     Respon->SetUuid(uuid);
//     Respon->SetMethod("add");
//     Con->Send(Respon);
// }

//实际的业务处理函数
void Add(const Json::Value & params, Json::Value & result)
{
    DLOG("进入Add函数处理业务");
    int num1=params["nums1"].asInt();
    int num2=params["nums2"].asInt();
    result["Res"]=num1+num2;
}
int main()
{
    std::shared_ptr<Dispactch> DispPtr = std::make_shared<Dispactch>();
    // 1 构造服务描述对象和注册实际的Rpc请求的处理业务
    std::shared_ptr<RpcRouter::Rpc_Ruoter> RpcRouterPtr=std::make_shared<RpcRouter::Rpc_Ruoter>();
    std::unique_ptr<RpcRouter::ServerDescBuild> SDbuild = std::make_unique<RpcRouter::ServerDescBuild>();
    SDbuild->SetMethodName("Add");
    SDbuild->SetParamsDesc({"nums1", RpcRouter::PraType::INT});
    SDbuild->SetParamsDesc({"nums2", RpcRouter::PraType::INT});
    SDbuild->SetCallBack(Add);
    SDbuild->SetReturnType(RpcRouter::PraType::INT);
    RpcRouterPtr->RegisterSerDec(SDbuild->bulid());

    //2 请求派发模块注册Rpc请求对应的回调函数-- RpcRouterPtr对象中的成员函数去处理Rpc请求
    DispPtr->RegisterHander(Rs_RqType::RPC_Req,[RpcRouterPtr](const Abstract::BaseConnection::ConPtr &Con, const Abstract::BaseMessage::MesaPtr &Mptr){
        RpcRouterPtr->DealRpcQuest(Con,Mptr);
    });

    // DispPtr->RegisterHander(Rs_RqType::RPC_Req, Rpc_ReqMessageCBack);
    // DispPtr->RegisterHander(Rs_RqType::TOPIC_Req, Topic_ReqMessageCBack);
    // DispPtr->RegisterHander(Rs_RqType::Service_Req, Ser_ResMessageCBack);
    auto sptr = Net::BulidMuduoServer::bulid(8080);
    sptr->SetReady_CallBack(
        [DispPtr](const Abstract::BaseConnection::ConPtr &Con, const Abstract::BaseMessage::MesaPtr &Mptr)
        {
            DispPtr->MessageCallback(Con, Mptr);
        }); // 设置服务端接收一条完整
    sptr->start();
}